package com.banciyuan.wallpaper.toast;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.banciyuan.wallpaper.R;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hfxt2008 on 14-8-22.
 * 自定义tosat样式，所有toast都是这个。
 */
public class MyToast {

    private static Map<String, Toast> mMap = new HashMap<String, Toast>();

    private static int WAIT_TIME = 2000;

    public static void show(Context mContext, String content, int rId) {

        if (mContext == null) return;
        if (TextUtils.isEmpty(content)) return;

        try {
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            View layout = inflater.inflate(R.layout.mytoast_layout, null);
            ImageView mIcon = (ImageView) layout.findViewById(R.id.toast_icon_img);
            TextView mContent = (TextView) layout.findViewById(R.id.toast_content_tx);
            Toast toast = new Toast(mContext.getApplicationContext());
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.setDuration(Toast.LENGTH_SHORT);
            mContent.setText(content);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mIcon.setImageDrawable(mContext.getResources().getDrawable(rId, mContext.getTheme()));
            } else {
                mIcon.setImageDrawable(mContext.getResources().getDrawable(rId));
            }
            toast.setView(layout);
            addToMap(content, toast);
            //toast.show();
        } catch (Exception e) {
            return;
        }
    }

    public static void show(Activity mContext, String content) {
        if (mContext == null) return;
        if (TextUtils.isEmpty(content)) return;

        try {
            LayoutInflater inflater =  mContext.getLayoutInflater();
            View layout = inflater.inflate(R.layout.mytoast_layout, null);
            TextView mContent = (TextView) layout.findViewById(R.id.toast_content_tx);
            Toast toast = new Toast(mContext.getApplicationContext());
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.setDuration(Toast.LENGTH_SHORT);
            mContent.setText(content);
            toast.setView(layout);
            addToMap(content, toast);
            //toast.show();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }

    public static void showLong(Context mContext, String content) {
        if (mContext == null) return;
        if (TextUtils.isEmpty(content)) return;

        try {
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            View layout = inflater.inflate(R.layout.mytoast_layout, null);
            TextView mContent = (TextView) layout.findViewById(R.id.toast_content_tx);
            Toast toast = new Toast(mContext.getApplicationContext());
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            mContent.setText(content);
            toast.setView(layout);
            addToMap(content, toast);
            //toast.show();
        } catch (Exception e) {
            return;
        }
    }

    public static void addToMap(final String content, Toast toast) {

        if (null == mMap.get(content)) {
            mMap.put(content, toast);
            toast.show();

            Handler handler = new Handler();

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mMap.remove(content);
                }
            }, WAIT_TIME);
        }
    }
}
